#include "mainviewimpl.h"

CMainViewImpl::CMainViewImpl(void)
{
}
CMainViewImpl::~CMainViewImpl(void)
{
    logi("CMainViewImpl::~CMainViewImpl");
}
HRESULT CMainViewImpl::Init(IBase* pRot, IBase* pBase)
{
    HRESULT hr = S_OK;
    rc_error(pRot != NULL, E_FAIL);
    hr = pRot->QueryInterface(IID_IRot, (void**)&m_pRot);
    rc_error(hr == S_OK, E_FAIL);

    hr = pBase->QueryInterface(IID_IMainRun, (void**)&m_pMainRun);
    rc_error(hr == S_OK, E_FAIL);

    return hr;
}
HRESULT CMainViewImpl::Uninit()
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CMainViewImpl::Start(_pid_t pid, unsigned int type)
{
    HRESULT hr = S_OK;

    hr = m_pRot->GetObject(CLSID_IUIbase, IID_IUIBase, (IBase**)&m_pIUiBase);
    rc_error(hr == S_OK, E_FAIL);
    rc_error(m_pIUiBase.m_p != INULL, E_FAIL);

    hr = m_pRot->GetObject(CLSID_CLogs, IID_ILogs, (IBase**)&m_pILogs);
    rc_error(hr == S_OK, E_FAIL);
    rc_error(m_pILogs.m_p != INULL, E_FAIL);

    hr = m_pIUiBase->CreateControl(UI_MESSAGELOOP, (IBase**)&m_pIUiMessageLoop);
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pIUiBase->CreateControl(UI_WINDOWS, (IBase**)&m_pIUiWindow);
    rc_error(hr == S_OK, E_FAIL);

    basic_tchar szPath[MAX_PATH + 1] = {0x00};
    get_executepath(pid_null, szPath, MAX_PATH);

    path_string strResFile(szPath);

    // strResFile = _T("C:\\app\\duilib\\bin\\");
    strResFile += _T("//res");
    // error pid_instance

    hr = m_pIUiWindow->SetResource(pid_instance, strResFile.c_str());
    rc_error(hr == S_OK, E_FAIL);

    _lComPtr<IBase> pBase;
    this->QueryInterface(IID_IBase, (void**)&pBase);

    m_pIUiWindow->BindEvent(pBase);

    hr = m_pIUiWindow->SetResourceFile(_T("main.xml"));
    rc_error(hr == S_OK, E_FAIL);

    m_pIUiWindow->CreateUiWindow(NULL);

    hr = m_pIUiMessageLoop->Add(m_pIUiWindow);
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pIUiMessageLoop->MessageLoop();
    rc_error(hr == S_OK, E_FAIL);

    hr = m_pIUiWindow->UnBindEvent();
    rc_error(hr == S_OK, E_FAIL);

    return hr;
}
HRESULT CMainViewImpl::Stop(unsigned int exit)
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CMainViewImpl::OnMsgStart()
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CMainViewImpl::OnMsgStop()
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CMainViewImpl::OnMsgSend(CLSID sn, CLSID tn, UINT msg, IBase* inmsg, IBase** outmsg)
{
    HRESULT hr = S_OK;

    return hr;
}
HRESULT CMainViewImpl::OnMsgRecv(CLSID sn, CLSID tn, UINT msg, IBase* inmsg, IBase** outmsg)
{
    HRESULT hr = S_OK;

    return hr;
}

ULONG CMainViewImpl::GetRoute()
{
    return 0;
}
ULONG CMainViewImpl::GetSub()
{
    return 0;
}
HRESULT CMainViewImpl::Set(ULONG ad)
{
    return S_OK;
}

HRESULT CMainViewImpl::OnUiEvent(const NotifyEvent* event)
{
    string strMsg = "hello,world";

    SafeStringPtr appname;
    appname.ptr = "app";
    appname.len = s_strlen("app");

    return m_pMainRun->SendBroadcastMsg(IID_INULL, CLSID_IMainViewImpl, IID_INULL, 100, NULL);
}
